#include <iostream>
#include <cstring>

using namespace std;

const int N = 210, INF = 0x3f3f3f3f;

int t[N];
int n, m;
int f[N][N];

void floyd(int k)
{
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			f[i][j] = min(f[i][j], f[i][k] + f[j][k]);
}

int main()
{
	cin >> n >> m;
	for(int i = 0; i < n; i++) cin >> t[i];
	memset(f, 0x3f, sizeof f);
	for(int i = 0; i < n; i++) f[i][i] = 0;
	for(int i = 1; i <= m; i++)
	{
		int a, b, c; cin >> a >> b >> c;
		f[a][b] = f[b][a] = min(f[a][b], c);
	}
	int pos = 0;
	int T; cin >> T;
	while(T--)
	{
		int a, b, c; cin >> a >> b >> c;
		while(pos < n && t[pos] <= c) floyd(pos++);
		
		if(t[a] > c || t[b] > c || f[a][b] == INF)  cout << -1 << endl;
		else cout << f[a][b] << endl; 
	}
	return 0;
}
